package com.promis.widget.mediapicker.ui;


import android.content.Context;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.promis.util.CollectionUtils;
import com.promis.util.StringUtils;
import com.promis.widget.R;
import com.promis.widget.mediapicker.MediaPickerConfig;
import com.promis.widget.mediapicker.entity.ImageParcelable;
import com.promis.widget.mediapicker.entity.MediaParcelable;
import com.promis.widget.mediapicker.manager.MediaPickerConstants;
import com.promis.widget.mediapicker.util.FileUtils;
import com.promis.widget.mediapicker.util.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dmcBig on 2017/7/5.
 */

public class ImageGridAdapter extends RecyclerView.Adapter<ImageGridAdapter.MyViewHolder>{

    private List<ImageParcelable> mediaList, selectedList;
    private Context context;
    private long maxFileCount, maxFileSize;
    public ImageGridAdapter(Context context, int maxFileCount, long maxFileSize){
        this.maxFileCount = maxFileCount;
        this.maxFileSize = maxFileSize;
        this.context=context;
        this.selectedList = new ArrayList<>();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{

        public ImageView media_image,check_image;
        public View mask_view;
        public TextView textView_time;
        public TextView textView_size;
        public LinearLayout video_info;

        public MyViewHolder(View view){
            super(view);
            media_image = (ImageView) view.findViewById(R.id.media_image);
            check_image = (ImageView) view.findViewById(R.id.check_image);
            mask_view =  view.findViewById(R.id.mask_view);
            video_info = (LinearLayout) view.findViewById(R.id.video_info);
            textView_size = (TextView) view.findViewById(R.id.textView_size);
            textView_time = (TextView) view.findViewById(R.id.textView_time);
            itemView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getItemWidth())); //让图片是个正方形

            media_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final ImageParcelable image = mediaList.get(getLayoutPosition());
                    int isSelect = isSelect(image);
                    if(selectedList.size() >= maxFileCount && isSelect < 0){
                        Toast.makeText(context,context.getString(R.string.msg_amount_limit),Toast.LENGTH_SHORT).show();
                    }else{
                        if(image.size > maxFileSize){
                            Toast.makeText(context,context.getString(R.string.msg_size_limit)+(FileUtils.fileSize(maxFileSize)),Toast.LENGTH_LONG).show();
                        }else {
                            mask_view.setVisibility(isSelect >= 0 ? View.INVISIBLE : View.VISIBLE);
                            check_image.setImageDrawable(isSelect >= 0 ? ContextCompat.getDrawable(context, R.mipmap.mediapicker_btn_unselected) : ContextCompat.getDrawable(context, R.mipmap.mediapicker_btn_selected));
                            setSelectMedias(image);
                            mOnItemClickListener.onItemClick(v, image, selectedList);
                        }
                    }

                }
            });
        }
    }

    int getItemWidth(){
        return (ScreenUtils.getScreenWidth(context) / MediaPickerConstants.GridSpanCount)- MediaPickerConstants.GridSpace;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_mediapicker_grid_item,viewGroup,false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        if (CollectionUtils.isNullOrEmpty(mediaList)) return;

        final ImageParcelable media = mediaList.get(position);

        Uri mediaUri = null;
        if (!StringUtils.isNullOrEmpty(media.localPath)) {
            mediaUri = Uri.parse("file://" + media.localPath);
        } else if (!StringUtils.isNullOrEmpty(media.httpOriginalPath)) {
            mediaUri = Uri.parse(MediaPickerConfig.httpUrl + media.httpOriginalPath);
        } else if (!StringUtils.isNullOrEmpty(media.compressPath)) {
            mediaUri = Uri.parse("file://" + media.compressPath);
        }
        Glide.with(context).load(mediaUri).into(holder.media_image);
//        holder.media_image.setImageURI(mediaUri);

        int isSelect=isSelect(media);
        holder.mask_view.setVisibility(isSelect>=0?View.VISIBLE:View.INVISIBLE);
        holder.check_image.setImageDrawable(isSelect>=0? ContextCompat.getDrawable(context,R.mipmap.mediapicker_btn_selected):ContextCompat.getDrawable(context,R.mipmap.mediapicker_btn_unselected));
        if (maxFileCount == 1) holder.check_image.setVisibility(View.GONE);
    }



    public void setSelectMedias(ImageParcelable image){
        int index=isSelect(image);
        if(index==-1){
            selectedList.add(image);
        }else{
            selectedList.remove(index);
        }
    }

    /**
     *
     * @param media
     * @return 大于等于0 就是表示以选择，返回的是在selectMedias中的下标
     */
    public int isSelect(MediaParcelable media){
        int is=-1;
        if(selectedList.size()<=0){
            return is;
        }
        for(int i=0;i<selectedList.size();i++){
            MediaParcelable m = selectedList.get(i);
            if(m.localPath.equals(media.localPath)){
                is=i;
                break;
            }
        }
        return is;
    }

    public void updateSelectAdapter(List<ImageParcelable> selectedList){
        if(selectedList!=null){
            this.selectedList = selectedList;
        }
        notifyDataSetChanged();
    }

    public void notifyDataSetChanged(List<ImageParcelable> mediaList){
        this.mediaList = mediaList;
        notifyDataSetChanged();
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public List<ImageParcelable> getSelectedImageList(){
        return selectedList;
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isNullOrEmpty(mediaList)) return 0;
        else return mediaList.size();
    }

    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, ImageParcelable image, List<ImageParcelable> selectMedias);
    }
}
